package com.imddy.spbatch001.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/jobLauncher")
public class JobLauncherController {
    private static final Logger log = LoggerFactory.getLogger(JobLauncherController.class);


    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job myJob;

    @RequestMapping("/jobLauncher.html")
    public void handle() throws Exception{
        log.info("开始执行任务...");
        JobParameters jobParameters;
        JobParametersBuilder jobParametersBuilder = new JobParametersBuilder()
                .addString("date1", LocalDate.now().toString())
                .addString("datetime1", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
                .addLocalDate("date", LocalDate.now())
                .addLocalDateTime("datetime", LocalDateTime.now());
        log.info("参数：{}", jobParametersBuilder);
        jobParameters = jobParametersBuilder.toJobParameters();
        log.info("参数：{}", jobParameters);
        jobLauncher.run(myJob, jobParameters);
    }

}